package com.usian.admin;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.Date;


@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class AdChannelTestDemo {

    @Autowired
    private AdChannelMapper adChannelMapper;

    @Autowired
    MockMvc mvc; // 执行请求
    @Autowired
    ObjectMapper mapper;  //封装对象


    //使用mockMVC测试 分页查询
    @Test
    public void savesFromMockMvc() throws Exception {
        ChannelDto dto = new ChannelDto();
        dto.setPage(1);
        dto.setSize(10);
        // post get put delete
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/api/v1/adChannel/findAll");
        builder.contentType(MediaType.APPLICATION_JSON_VALUE).content(mapper.writeValueAsBytes(dto));
        mvc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());
    }
    //  添加
    @Test
    public void add() throws Exception {
        AdChannel adChannel=new AdChannel();
        adChannel.setName("123");
        adChannel.setCreatedTime(new Date());
        adChannel.setDescription("11");
        adChannel.setOrd(1);
        adChannel.setIsDefault(true);
        adChannel.setStatus(true);
        // post get put delete
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/api/v1/adChannel/add");
        builder.contentType(MediaType.APPLICATION_JSON_VALUE).content(mapper.writeValueAsBytes(adChannel));
        mvc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());
    }

    //回显
    @Test
    public void findById() throws Exception {
     Integer id=1;
        // post get put delete
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/api/v1/adChannel/findById/"+id);
        mvc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());
    }
    @Test
    public void update() throws Exception {
        AdChannel adChannel=new AdChannel();
        adChannel.setId(35);
        adChannel.setName("11111");
        adChannel.setCreatedTime(new Date());
        adChannel.setDescription("11");
        adChannel.setOrd(1);
        adChannel.setIsDefault(true);
        adChannel.setStatus(true);
        // post get put delete
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/api/v1/adChannel/update");
        builder.contentType(MediaType.APPLICATION_JSON_VALUE).content(mapper.writeValueAsBytes(adChannel));
        mvc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());
    }

    @Test
    public void delete() throws Exception {
        Integer id=35;
        // post get put delete
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/api/v1/adChannel/delete/"+id);

        mvc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());
    }

}
